from flask import jsonify, Response, Blueprint, make_response
from app import app, mysql
import json

# 创建路由蓝图
student_bp = Blueprint('student_bp', __name__)


# 查询学生列表
@student_bp.route('/api/students', methods=['GET'])
def get_students():
    cur = mysql.connection.cursor()
    cur.execute("SELECT * FROM student_info")
    rv = cur.fetchall()
    students = []
    for row in rv:
        student = {
            'id': row[0],
            'name': row[1],
            'age': row[2]
        }
        students.append(student)
    cur.close()
    # 解决中文乱码一
    response_data = {
        "success": True,
        "code": 200,
        "msg": "成功",
        "data": students
    }
    response = Response(
        response=json.dumps(response_data, ensure_ascii=False),
        status=200,
        mimetype='application/json'
    )
    response.headers['Content-Type'] = 'application/json; charset=utf-8'
    return response
    # return jsonify(students), 200, {'Content-Type': 'application/json; charset=utf-8'}


# 注册路由蓝图
app.register_blueprint(student_bp)
